print("haha-tk-ss插件，版本：0.0.1.IBIOVS")

local wowWatcher = CreateFrame("Frame", "BlueSquareFrame", UIParent);-- 创建一个新的框架
wowWatcher:SetFrameLevel(1);-- 设置框架的层级
wowWatcher:SetSize(42, 5) -- 宽度和高度都是100像素 -- 设置框架的尺寸
wowWatcher:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 0, 0) -- 距离屏幕左上角10,-10的位置 -- 设置框架的位置

-- 色块大小
local colorSize = 5;

-- 1-1 主目标暗影箭，红色不打，绿色打
-- 1-2 焦点暗影箭，红色不打，绿色打

-- 1-1 主目标暗影箭，红色不打，绿色打
local target_ayj_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
target_ayj_green:SetColorTexture(0, 1, 0, 1)
target_ayj_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 0, 0);
target_ayj_green:SetSize(colorSize, colorSize);
target_ayj_green:SetAlpha(0);

local target_ayj_red = wowWatcher:CreateTexture(nil, "BACKGROUND")
target_ayj_red:SetColorTexture(1, 0, 0, 1)
target_ayj_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 0, 0);
target_ayj_red:SetSize(colorSize, colorSize)
target_ayj_red:SetAlpha(1)

-- 间隔纹理
local splitTexture1 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture1:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture1:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 5, 0);
splitTexture1:SetSize(1, colorSize);
splitTexture1:SetAlpha(1);

-- 1-2 焦点暗影箭，红色不打，绿色打
local focus_ayj_green = wowWatcher:CreateTexture(nil, "BACKGROUND")
focus_ayj_green:SetColorTexture(0, 1, 0, 1)
focus_ayj_green:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 6, 0);
focus_ayj_green:SetSize(colorSize, colorSize);
focus_ayj_green:SetAlpha(0);

local focus_ayj_red = wowWatcher:CreateTexture(nil, "BACKGROUND")
focus_ayj_red:SetColorTexture(1, 0, 0, 1)
focus_ayj_red:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 6, 0);
focus_ayj_red:SetSize(colorSize, colorSize)
focus_ayj_red:SetAlpha(1)

-- 间隔纹理
local splitTexture2 = wowWatcher:CreateTexture(nil, "BACKGROUND")
splitTexture2:SetColorTexture(0.5, 0.5, 0.5, 1)
splitTexture2:SetPoint("TOPLEFT", wowWatcher, "TOPLEFT", 11, 0);
splitTexture2:SetSize(1, colorSize);
splitTexture2:SetAlpha(1);

function checkTargetAyj()
    local name, _, _, _, _, expirationTime = AuraUtil.FindAuraByName("暗影之拥", "target", "PLAYER|HARMFUL")
    if not name then
        target_ayj_red:SetAlpha(0);
        target_ayj_green:SetAlpha(1);
        return;
    end

    local leftTime = expirationTime - GetTime();
    if leftTime < 5 then
        target_ayj_red:SetAlpha(0);
        target_ayj_green:SetAlpha(1);
        return;
    end

    target_ayj_red:SetAlpha(1);
    target_ayj_green:SetAlpha(0);
end

function checkFocusAyj()
    local name, _, _, _, _, expirationTime = AuraUtil.FindAuraByName("暗影之拥", "focus", "PLAYER|HARMFUL")
    if not name then
        focus_ayj_red:SetAlpha(0);
        focus_ayj_green:SetAlpha(1);
        return;
    end

    local leftTime = expirationTime - GetTime();
    if leftTime < 5 then
        focus_ayj_red:SetAlpha(0);
        focus_ayj_green:SetAlpha(1);
        return;
    end

    focus_ayj_red:SetAlpha(1);
    focus_ayj_green:SetAlpha(0);
end

wowWatcher:SetScript("OnUpdate", function()
    checkTargetAyj();
    checkFocusAyj();
end);

local AYJ_SPELL_ID = 47809  -- 暗影箭技能ID
local isCasting = false       -- 是否正在施法中
local castStartTime = 0      -- 施法开始时间
local castingTarget = nil    -- 正在施法的目标（"target"/"focus"）

-- 事件处理
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("UNIT_SPELLCAST_START")
eventFrame:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
eventFrame:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
eventFrame:RegisterEvent("UNIT_SPELLCAST_FAILED")
eventFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

eventFrame:SetScript("OnEvent", function(_, event, ...)
    if event == "UNIT_SPELLCAST_START" then
        local unit, _, spellID, _ = ...
        if unit == "player" and spellID == AYJ_SPELL_ID then
            print("11111111111")
            isCasting = true
            castStartTime = GetTime()
            -- 判断施法目标类型
            if UnitExists("target") and UnitIsUnit("target", GetMouseFocus()) then
                castingTarget = "target"
                target_ayj_red:SetAlpha(1);
                target_ayj_green:SetAlpha(0);
            elseif UnitExists("focus") then
                castingTarget = "focus"
                focusIndicator.green:SetAlpha(0)
                focusIndicator.red:SetAlpha(1)
            end
        end

    elseif event == "UNIT_SPELLCAST_SUCCEEDED" then
        local unit, _, spellID = ...
        if unit == "player" and spellID == AYJ_SPELL_ID then
            -- 设置定时器解除锁定
            C_Timer.After(CAST_LOCK_DURATION, function()
                isCasting = false
                castingTarget = nil
            end)
        end

    elseif event:find("INTERRUPTED") or event:find("FAILED") then
        isCasting = false
        castingTarget = nil
        updateIndicators()

    elseif event == "COMBAT_LOG_EVENT_UNFILTERED" then
        local _, subEvent, _, _, _, _, _, destGUID, _, _, _, spellID = ...
        -- 暗影箭命中时立即刷新
        if subEvent == "SPELL_DAMAGE" and spellID == AYJ_SPELL_ID then
            if castingTarget == "target" and destGUID == UnitGUID("target") then
                targetIndicator.red:SetAlpha(0)
            elseif castingTarget == "focus" and destGUID == UnitGUID("focus") then
                focusIndicator.red:SetAlpha(0)
            end
            updateIndicators()
        end
    end
end)
